﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DbfExplorer
{
    public partial class DbfForm : Form
    {
        private DbfDotNet.DbfTable mDbfTable;

        public DbfForm()
        {
            InitializeComponent();
        }

        private void DbfForm_Load(object sender, EventArgs e)
        {

        }

        public DbfDotNet.DbfTable DbfTable
        {
            get {
                return mDbfTable;
            }
            set {
                if (value == mDbfTable) return;
                mDbfTable = value;
                dbfTableView1.DbfTable = value;
                textBox1.Clear();
                while (splitContainer1.Panel1.Controls.Count>0)
                {
                    splitContainer1.Panel1.Controls[0].Dispose();
                }
                if (value != null)
                {
                    var sb = new StringBuilder();
                    sb.AppendLine("using DbfDotNet;");
                    sb.AppendLine("");
                    sb.AppendLine("namespace yournamespace");
                    sb.AppendLine("{");
                    sb.AppendLine("   public class yourtable : DbfRecord");
                    sb.AppendLine("   {");
                    for (int i = 0; i < mDbfTable.Columns.Count; i++)
                    {
                        var col = mDbfTable.Columns[i];
                        var colType = (col.NativeType == null ? "unknown" : col.NativeType.Name);
                        var columnName = col.ColumnName;
                        sb.AppendLine(string.Format("      public {0} {1}", colType, columnName));
                        DbfColumn column = new DbfColumn();
                        column.label1.Text = col.ColumnName;
                        column.Dock = DockStyle.Top;
                        splitContainer1.Panel1.Controls.Add(column);
                        column.BringToFront();

                    }
                    sb.AppendLine("   }");
                    sb.AppendLine("}");
                    textBox1.Text = sb.ToString();
                }
            }

        }
    }
}
